
import java.sql.*;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author William
 */
public class Staff {
    public int staffID;
    public String loginName;
    private String password; 

    private Connection connect;

    /*
    public Staff(int staffID, Connection connect) {
        this.staffID = staffID;
        this.connect = connect;
        loadFromDB();
    }
     * 
     */
    
    public Staff(String loginName, String password){
        this.loginName = loginName;
        this.password = password;
        
    }
    
    public boolean addToDB(Connection connect){
        try {
            String addQuery = "INSERT INTO DORMS.staff (loginName, password) VALUES (?,?)";
            PreparedStatement addStmt = connect.prepareStatement(addQuery);
            addStmt.setString(1, loginName);
            addStmt.setString(2, password);
            addStmt.execute();
            
            
            Statement stmt = connect.createStatement();
            ResultSet result = stmt.executeQuery("SELECT last_insert_id() from DORMS.staff");
            if (result.next())
                staffID = result.getInt("last_insert_id()");
            
            return true;
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to add to database. Please check for duplicates");
             
        }
        return false;
        
        
    }
    
    
    public static boolean logIn(String loginName, String password, Connection connect){
        try{
            String checkLoginQuery = "SELECT * FROM staff where loginName = ? AND password = ?";
            PreparedStatement loginStmt = connect.prepareStatement(checkLoginQuery);
            loginStmt.setString(1, loginName);
            loginStmt.setString(2, password);
            ResultSet result = loginStmt.executeQuery();
            if(result.next()){
                return true;
            }else
                return false;
            
        } catch(Exception ex){
            JOptionPane.showMessageDialog(null, "Unable to check for login. Please try again.");
            
        }
        
        return false;
    }
    /*
    private void loadFromDB(){
        try {
            String loadQuery = "select * from staff where staffID = ?";
            PreparedStatement loadStmt = connect.prepareStatement(loadQuery);
            loadStmt.setInt(1, staffID);
            ResultSet studentInfo = loadStmt.executeQuery();
            if(studentInfo.next()){
                loginName = studentInfo.getString("loginName");
                password = studentInfo.getString("password");
            }
            
            
        } catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to load student from database.");
            
        }
    }
     * 
     */

    
}